//
//  FriendNewTableViewCell.m
//  CheekiOS
//
//  Created by mac on 2017/6/13.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "FriendNewTableViewCell.h"
#import "SDTimeLineCellModel.h"
#import "UIView+SDAutoLayout.h"
#import "SDWeiXinPhotoContainerView.h"
#import "SDTimeLineCellOperationMenu.h"

const CGFloat FcontentLabelFontSize = 15;
CGFloat FmaxContentLabelHeight = 0; // 根据具体font而定

NSString *const FriendNewButtonClickedNotification = @"FriendNewButtonClickedNotification";

@implementation FriendNewTableViewCell{

    
    UIButton *_deleteButton;
    UIButton *_playButton;
    UILabel *_line;
    UIButton *_iconButton;
    UIImageView *_iconView;
    UILabel *_locationLable;
    UILabel *_nameLable;
    UILabel *_contentLabel;
    UILabel *_detailLabel;
    UILabel *_dateLabel;
    UILabel *_placeLabel;
    UILabel *_payLabel;
    UILabel *_sexLabel;
    UILabel *_arriveLabel;
    UILabel *_activityLabel;
    UIImageView *_videoImageView;
    SDWeiXinPhotoContainerView *_picContainerView;
    UILabel *_timeLabel;
    UIButton *_moreButton;
    FriendNewLineCellCommentView *_commentView;
    SDTimeLineCellOperationMenu *_operationMenu;
    BOOL _isTryst;


}

+(instancetype)cellWithTableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"FriendNewTableViewCellident";
    FriendNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    
    if (!cell) {
        cell = [[FriendNewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(FOperationButtonClickedNotification:) name:FriendNewButtonClickedNotification object:nil];
        _line = [UILabel new];
        _line.backgroundColor = TheLinColor;
        _iconView = [UIImageView new];
        _iconView.layer.cornerRadius = 25;
        _iconView.layer.masksToBounds = YES;
        _iconButton = [UIButton new];
        _iconButton.backgroundColor = [UIColor clearColor];
        [_iconButton addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];
        
        _deleteButton = [UIButton new];
        _deleteButton.titleLabel.font = [UIFont systemFontOfSize:13];
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteButton setTitleColor:self.tintColor forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(FriendNewclickDeleteButton) forControlEvents:UIControlEventTouchUpInside];
        
        _locationLable = [UILabel new];
        _locationLable.font = [UIFont systemFontOfSize:13];
        _locationLable.textColor = [UIColor colorWithRed:(54 / 255.0) green:(71 / 255.0) blue:(121 / 255.0) alpha:0.9];
        
        _nameLable = [UILabel new];
        _nameLable.font = [UIFont systemFontOfSize:14];
        _nameLable.textColor = [UIColor colorWithRed:(54 / 255.0) green:(71 / 255.0) blue:(121 / 255.0) alpha:0.9];
        
        _contentLabel = [UILabel new];
        _contentLabel.font = [UIFont systemFontOfSize:FcontentLabelFontSize];
        _videoImageView = [UIImageView new];
        _contentLabel.numberOfLines = 0;
        
        if (FmaxContentLabelHeight == 0) {
            FmaxContentLabelHeight = _contentLabel.font.lineHeight * 3;
        }
        
        _detailLabel = [UILabel new];
        _detailLabel.font = [UIFont systemFontOfSize:13];
        _detailLabel.textColor = [UIColor lightGrayColor];
        _detailLabel.numberOfLines = 0;
        _payLabel = [UILabel new];
        _payLabel.font = [UIFont systemFontOfSize:13];
        _payLabel.textColor = [UIColor lightGrayColor];
        _payLabel.numberOfLines = 0;
        _sexLabel = [UILabel new];
        _sexLabel.font = [UIFont systemFontOfSize:13];
        _sexLabel.textColor = [UIColor lightGrayColor];
        _sexLabel.numberOfLines = 0;
        _arriveLabel = [UILabel new];
        _arriveLabel.font = [UIFont systemFontOfSize:13];
        _arriveLabel.textColor = [UIColor lightGrayColor];
        _arriveLabel.numberOfLines = 0;
        _dateLabel = [UILabel new];
        _dateLabel.font = [UIFont systemFontOfSize:13];
        _dateLabel.textColor = [UIColor lightGrayColor];
        _dateLabel.numberOfLines = 0;
        _activityLabel = [UILabel new];
        _activityLabel.font = [UIFont systemFontOfSize:13];
        _activityLabel.textColor = [UIColor lightGrayColor];
        _activityLabel.numberOfLines = 0;
        _placeLabel = [UILabel new];
        _placeLabel.font = [UIFont systemFontOfSize:13];
        _placeLabel.textColor = [UIColor lightGrayColor];
        _placeLabel.numberOfLines = 0;
        
        _moreButton = [UIButton new];
        [_moreButton setTitle:@"全文" forState:UIControlStateNormal];
        [_moreButton setTitleColor:TimeLineCellHighlightedColor forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(moreButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        _moreButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        _operationButton = [UIButton new];
        [_operationButton setImage:[UIImage imageNamed:@"Friend_news_comment_button"] forState:UIControlStateNormal];
        [_operationButton addTarget:self action:@selector(operationButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        
        
        _picContainerView = [SDWeiXinPhotoContainerView new];
        _picContainerView.delegate = self;
        
        _commentView = [FriendNewLineCellCommentView new];
        _commentView.delegate = self;
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:13];
        
        
        _operationMenu = [SDTimeLineCellOperationMenu new];
        __weak typeof(self) weakSelf = self;
        //点赞
        [_operationMenu setLikeButtonClickedOperation:^(NSString *opertionStr){
            NSString *str = opertionStr;
            if ([weakSelf.delegate respondsToSelector:@selector(FriendNewdidClickLikeButtonInCell:opertionStr:)]) {
                [weakSelf.delegate FriendNewdidClickLikeButtonInCell:weakSelf opertionStr:str];
            }
        }];
        
        
        [_operationMenu setCommentButtonClickedOperation:^{
            if ([weakSelf.delegate respondsToSelector:@selector(FriendNewdidClickcCommentButtonInCell:)]) {
                [weakSelf.delegate FriendNewdidClickcCommentButtonInCell:weakSelf];
            }
        }];
        
        _playButton = [UIButton new];
        NSArray *views = @[_iconView, _nameLable, _contentLabel, _moreButton, _detailLabel, _payLabel, _sexLabel, _arriveLabel, _dateLabel, _activityLabel, _placeLabel, _picContainerView, _timeLabel, _operationButton, _operationMenu, _commentView,_line,_iconButton,_deleteButton,_locationLable,_videoImageView,_playButton];
        
        [self.contentView sd_addSubviews:views];

        
    }
    return self;
}

-(void)setModel:(Active *)model{

    _model = model;
    _picContainerView.Activemodel = model;
    UIView *contentView = self.contentView;
    CGFloat margin = 10;
    
    _iconView.sd_layout
    .leftSpaceToView(contentView, margin)
    .topSpaceToView(contentView, margin + 5)
    .widthIs(50)
    .heightIs(50);
    
    _iconButton.sd_layout
    .leftSpaceToView(contentView, margin)
    .topSpaceToView(contentView, margin + 5)
    .widthIs(50)
    .heightIs(50);
    
    _nameLable.sd_layout
    .leftSpaceToView(_iconView, margin)
    .topEqualToView(_iconView)
    .heightIs(18);
    [_nameLable setSingleLineAutoResizeWithMaxWidth:200];
    
    _contentLabel.sd_layout
    .leftEqualToView(_nameLable)
    .topSpaceToView(_nameLable, margin)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    // morebutton的高度在setmodel里面设置
    _moreButton.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_contentLabel, 0)
    .widthIs(30);
    
    _detailLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _payLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _sexLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _arriveLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _dateLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _activityLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _placeLabel.sd_layout
    .leftEqualToView(_nameLable)
    .rightSpaceToView(contentView, margin)
    .autoHeightRatio(0);
    
    _playButton.sd_layout
    .leftEqualToView(_nameLable);
    
    _picContainerView.sd_layout
    .leftEqualToView(_contentLabel); // 已经在内部实现宽度和高度自适应所以不需要再设置宽度高度，top值是具体有无图片在setModel方法中设置
    
    _timeLabel.sd_layout
    .leftEqualToView(_contentLabel)
    .topSpaceToView(_picContainerView, margin)
    .heightIs(15);
    [_timeLabel setSingleLineAutoResizeWithMaxWidth:200];
    
    _locationLable.sd_layout
    .rightSpaceToView(contentView, margin)
    .leftEqualToView(_contentLabel);
    
    _deleteButton.sd_layout
    .leftSpaceToView(_timeLabel, 10)
    .topEqualToView(_timeLabel)
    .widthIs(80)
    .heightIs(15);
    
    _operationButton.sd_layout
    .rightSpaceToView(contentView, margin)
    .centerYEqualToView(_timeLabel)
    .heightIs(25)
    .widthIs(25);
    
    _commentView.sd_layout
    .leftSpaceToView(self.contentView, margin)
    .rightSpaceToView(self.contentView, margin)
    .topSpaceToView(_locationLable, margin); // 已经在内部实现高度自适应所以不需要再设置高度
    
    _operationMenu.sd_layout
    .rightSpaceToView(_operationButton, 0)
    .heightIs(36)
    .centerYEqualToView(_operationButton)
    .widthIs(0);
    
    
    _line.sd_layout
    .leftEqualToView(contentView)
    .rightEqualToView(contentView)
    .heightIs(1);

    
    if ([@(model.uid).stringValue isEqualToString:[HQHttpManager getUserId]]) {
        _deleteButton.hidden = NO;
    } else {
        _deleteButton.hidden = YES;
    }
    
    [_commentView setupWithLikeItemsArray:model.praiseList commentItemsArray:model.commentList];
    [_operationMenu setupWithLikeOperationMenu:model.praiseList];
    if(model.photo){
        [_iconView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.photo)] placeholderImage:[UIImage imageNamed:@"llustration"]];
    }
    _nameLable.text = model.nickName;
    // 防止单行文本label在重用时宽度计算不准的问题
    // [_nameLable sizeToFit];
    
    

    
    _detailLabel.text = model.hopeNote;
 
    _dateLabel.text = model.trystTime;
    _activityLabel.text = model.funName;
    _placeLabel.text = model.trystPlace;
    if (model.video.length) {
        _picContainerView.picPathStringsArray = @[];
    } else {
        _picContainerView.picPathStringsArray = [model.img componentsSeparatedByString:@","];
    }
    
    if (model.shouldShowMoreButton) { // 如果文字高度超过60
        _moreButton.sd_layout.heightIs(20);
        _moreButton.hidden = NO;
        if (model.isOpening) { // 如果需要展开
            _contentLabel.sd_layout.maxHeightIs(MAXFLOAT);
            [_moreButton setTitle:@"收起" forState:UIControlStateNormal];
        } else {
            _contentLabel.sd_layout.maxHeightIs(FmaxContentLabelHeight);
            [_moreButton setTitle:@"全文" forState:UIControlStateNormal];
        }
    } else {
        _moreButton.sd_layout.heightIs(0);
        _moreButton.hidden = YES;
    }
    if (model.hopeNote.length) {
        _detailLabel.sd_layout.topSpaceToView(_moreButton, 10.0);
        _detailLabel.hidden = NO;
    } else {
        _detailLabel.sd_layout.topSpaceToView(_moreButton, 0.0);
        _detailLabel.sd_layout.heightIs(0);
        _detailLabel.hidden = YES;
    }
    if (_payLabel.text.length) {
        _payLabel.sd_layout.topSpaceToView(_detailLabel, 10.0);
        _payLabel.hidden = NO;
    } else {
        _payLabel.sd_layout.topSpaceToView(_detailLabel, 0.0);
        _payLabel.sd_layout.heightIs(0);
        _payLabel.hidden = YES;
    }
    if (_sexLabel.text.length) {
        _sexLabel.sd_layout.topSpaceToView(_payLabel, 10.0);
        _sexLabel.hidden = NO;
    } else {
        _sexLabel.sd_layout.topSpaceToView(_payLabel, 0.0);
        _sexLabel.sd_layout.heightIs(0);
        _sexLabel.hidden = YES;
    }
    if (_arriveLabel.text.length) {
        _arriveLabel.sd_layout.topSpaceToView(_sexLabel, 10.0);
        _arriveLabel.hidden = NO;
    } else {
        _arriveLabel.sd_layout.topSpaceToView(_sexLabel, 0.0);
        _arriveLabel.sd_layout.heightIs(0);
        _arriveLabel.hidden = YES;
    }
    if (_dateLabel.text.length) {
        _dateLabel.sd_layout.topSpaceToView(_arriveLabel, 10.0);
        _dateLabel.hidden = NO;
    } else {
        _dateLabel.sd_layout.topSpaceToView(_arriveLabel, 0.0);
        _dateLabel.sd_layout.heightIs(0);
        _dateLabel.hidden = YES;
    }
    if (_activityLabel.text.length) {
        _activityLabel.sd_layout.topSpaceToView(_dateLabel, 10.0);
        _activityLabel.hidden = NO;
    } else {
        _activityLabel.sd_layout.topSpaceToView(_dateLabel, 0.0);
        _activityLabel.sd_layout.heightIs(0);
        _activityLabel.hidden = YES;
    }
    if (_placeLabel.text.length) {
        _placeLabel.sd_layout.topSpaceToView(_activityLabel, 10.0);
        _placeLabel.hidden = NO;
    } else {
        _placeLabel.sd_layout.topSpaceToView(_activityLabel, 0.0);
        _placeLabel.sd_layout.heightIs(0);
        _placeLabel.hidden = YES;
    }
    //播放器
    CGFloat videoTopMargin = 0;
    if (model.video.length) {
        videoTopMargin = 10;
        [_playButton setImage:[UIImage imageNamed:@"home_play_icon"] forState:UIControlStateNormal];
        _playButton.sd_layout
        .heightIs(180)
        .widthIs(120)
        .topSpaceToView(_placeLabel,videoTopMargin);
        _videoImageView.sd_layout
        .topEqualToView(_playButton)
        .bottomEqualToView(_playButton)
        .leftEqualToView(_playButton)
        .rightEqualToView(_playButton);
        if(model.img){
            [_videoImageView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.img)] placeholderImage:[UIImage imageNamed:@"placeholder"]];
        }
        _playButton.hidden = NO;
        [_playButton addTarget:self action:@selector(FriendNewclickPlayButton) forControlEvents:UIControlEventTouchUpInside];
    } else {
        _playButton.sd_layout
        .heightIs(0)
        .widthIs(120)
        .topSpaceToView(_placeLabel,videoTopMargin);
        _playButton.hidden = YES;
    }
    CGFloat picContainerTopMargin = 0;
    if (_picContainerView.picPathStringsArray.count) {
        picContainerTopMargin = 10;
    }
    _picContainerView.sd_layout.topSpaceToView(_playButton, picContainerTopMargin);
    
    UIView *bottomView;
    
    CGFloat locationTopMargin = 0;
    if (model.location.length) {
        locationTopMargin = 10;
        _locationLable.text = model.location;
        _locationLable.sd_layout
        .heightIs(15)
        .topSpaceToView(_timeLabel,locationTopMargin);
        _locationLable.hidden = NO;
    } else {
        _locationLable.sd_layout
        .heightIs(0)
        .topSpaceToView(_timeLabel,locationTopMargin);
        _locationLable.hidden = YES;
    }
    if (!model.commentList.count && !model.praiseList.count) {
        if (model.location.length) {
            bottomView = _locationLable;
        } else {
            bottomView = _timeLabel;
        }
    } else {
        bottomView = _commentView;
    }
    _line.sd_layout.topSpaceToView(bottomView, 14);
    [self setupAutoHeightWithBottomView:bottomView bottomMargin:15];
    
    _timeLabel.text = model.createTime;


}



- (void)setFrame:(CGRect)frame {
    [super setFrame:frame];
    if (_operationMenu.isShowing) {
        _operationMenu.show = NO;
    }
}

- (void)FriendNewclickDeleteButton {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(FriendNewdidClickDeleteInCell:)]) {
        [weakSelf.delegate FriendNewdidClickDeleteInCell:weakSelf];
    }
}

- (void)clickImage:(UIButton *)sender {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(FriendNewdidClickIconInCell:)]) {
        [weakSelf.delegate FriendNewdidClickIconInCell:weakSelf];
    }
}

#pragma mark - private actions
- (void)moreButtonClicked {
    if (self.FriendNewmoreButtonClickedBlock) {
        self.FriendNewmoreButtonClickedBlock(self.indexPath);
    }
}


#pragma mark =========点赞==========
- (void)operationButtonClicked {
    [self postOperationButtonClickedNotification];
    
    
    if (_isTryst&&![@(_model.uid).stringValue isEqualToString:[HQHttpManager getUserId]]) {

    } else {
        
        [self.contentView bringSubviewToFront:_operationMenu];
        _operationMenu.show = !_operationMenu.isShowing;
    }
}

- (void)FOperationButtonClickedNotification:(NSNotification *)notification {
    UIButton *btn = [notification object];
    if (btn != _operationButton && _operationMenu.isShowing) {
        _operationMenu.show = NO;
    }

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self postOperationButtonClickedNotification];
    if (_operationMenu.isShowing) {
        _operationMenu.show = NO;
    }

}



- (void)postOperationButtonClickedNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:FriendNewButtonClickedNotification object:_operationButton];
}
//播放代理
- (void)FriendNewclickPlayButton {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(FriendNewdidClickPlayButton:)]) {
        [weakSelf.delegate FriendNewdidClickPlayButton:weakSelf.model.video];
    }
}
//点击评论 回复

- (void)replycommentId:(NSString *)commentid username:(NSString *)username{
    self.FriendNewdidClickCommentLabelBlock(commentid, CGRectMake(0, 0, 0,0), _indexPath,username);
}
//个人信息里的状态删除
-(void)DeletePhotoAction{
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(UserEditDeleteInCell:)]) {
        [weakSelf.delegate UserEditDeleteInCell:weakSelf];
    }
    
}




- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
